import {Desk} from "./disPlayConfig";
import disPlayItem from "./disPlayItem";
import disFindItem from "./disFindItem";

const {ccclass, property} = cc._decorator;

@ccclass
export default class disPlayLayer extends cc.Component {

    @property(cc.Node)
    ndContextList: cc.Node = null;
    @property(cc.Prefab)
    pbDisplayList: cc.Prefab = null;
    @property(cc.Prefab)
    pbFindDisItem: cc.Prefab = null;
    @property(cc.Node)
    ndPos: cc.Node = null;
    @property(cc.Size)
    szContent: cc.Size = null;
    @property(cc.EditBox)
    editFind: cc.EditBox = null;
    @property(cc.Node)
    ndFindResult: cc.Node = null;
    @property(cc.Node)
    ndFindListContent: cc.Node = null;
    @property(cc.Label)
    lbSearchTitle: cc.Label = null;

    onLoad() {
        this.initDesk();
    }

    start() {
        this.ndFindResult.active = false;
    }

    initDesk() {
        const all = Desk.getDeskDisplayListInfo();
        for (let i = 0; i < all.length; i++) {
            let display = all[i];
            let node = cc.instantiate(this.pbDisplayList);
            this.ndContextList.addChild(node);
            let com = node.getComponent(disPlayItem);
            com.bindData(display);
            // const {width, height} = cc.view.getCanvasSize();
            const {x, y} = this.ndPos.getPosition();
            let xi = i % 3;
            let yj = Math.floor(i / 3);
            com.setMaxMinParam(
                cc.v2(x + 140 + xi * (this.szContent.width + 50), y - 100 - yj * (this.szContent.height + 80)),
                cc.v2(0, 0),
                cc.size(this.szContent.width / 750, this.szContent.height / 1334));
        }
    }

    onEditInputOver() {
        if (!this.editFind.string) {
            return;
        }
        this.ndFindResult.active = true;
        //
        let ids = Desk.searchIdsOfName(this.editFind.string);
        //
        this.lbSearchTitle.string = `"${this.editFind.string}" ${ids.length}个查找结果`;
        //
        for (let id of ids) {
            let node = cc.instantiate(this.pbFindDisItem);
            this.ndFindListContent.addChild(node);
            node.getComponent(disFindItem).setId(id);
        }
    }

    onTouchCloseFindResult() {
        this.ndFindListContent.destroyAllChildren();
        this.ndFindResult.active = false;
    }

    // update (dt) {}
}
